package com.community.shop.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 创建时间 自定义转换器
 */
public class CustomCreateTime implements Converter<Date> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Date.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 这里读的时候会调用
     *
     * @param context
     * @return
     */
    @Override
    public Date convertToJavaData(ReadConverterContext<?> context) {
//        long l = Long.parseLong("1674402136682");
//        Date date = new Date(l);

        BigDecimal numberValue = context.getReadCellData().getNumberValue();
        long l1 = numberValue.multiply(new BigDecimal("86400")).longValue();
        Instant instant = Instant.ofEpochSecond(l1 - 2209190400L);
        return Date.from(instant);
    }

    /**
     * 这里是写的时候会调用
     *
     * @return
     */
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Date> context) {
        Date value = context.getValue();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = format.format(value);
        return new WriteCellData<>(format1);
    }

}
